import 'package:flutter/material.dart';
import 'package:puppy_store/models/home_model.dart';
import 'package:puppy_store/pages/home/widgets/loadingBg.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:puppy_store/widgets/custom_image.dart';
import 'package:puppy_store/widgets/price_widget.dart';

class FreshGoodsWidget extends StatelessWidget {
  FreshGoodsWidget({this.freshGoods, super.key});

  List<FreshGoodsModel>? freshGoods;

  double _imgWidth = 0.0; // 用于存储图片的宽度，初始值为0.0，确保在第一次使用时被赋值，避免空指针异常。

  Widget _buildContents(List<FreshGoodsModel> freshGoods) {
    return StaggeredGridView.countBuilder(
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      crossAxisCount: freshGoods.length,
      itemCount: freshGoods.length,
      crossAxisSpacing: 20,
      itemBuilder: (BuildContext context, int index) {
        return Column(
          children: [
            // 图片
            // Image.network(
            //   freshGoods[index].picture!,
            //   width: _imgWidth,
            //   fit: BoxFit.cover,
            // ),
            CustomImage(
              url: freshGoods[index].picture!,
              width: _imgWidth,
              height: _imgWidth,
            ),
            // 标题
            Padding(
              padding: EdgeInsets.only(top: 5, bottom: 5),
              child: Text(
                freshGoods[index].name!,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  fontSize: 12,
                  color: Color(0xFF262626),
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
            // 价格
            PriceWidget(price: freshGoods[index].price!),
            // RichText(
            //   text: TextSpan(
            //     text: '￥',
            //     style: TextStyle(fontSize: 9, color: Color(0xFFff4400)),
            //     children: [
            //       TextSpan(
            //         text: freshGoods[index].price!.split('.')[0],
            //         style: TextStyle(fontSize: 13),
            //       ),
            //       TextSpan(text: '.', style: TextStyle(fontSize: 13)),
            //       TextSpan(
            //         text: freshGoods[index].price!.split('.')[1],
            //         style: TextStyle(fontSize: 12),
            //       ),
            //     ],
            //   ),
            // ),
          ],
        );
      },
      staggeredTileBuilder: (index) => StaggeredTile.fit(1),
    );
  }

  @override
  Widget build(BuildContext context) {
    // 计算图片宽度
    double screenWidth = MediaQuery.of(context).size.width.toDouble();
    _imgWidth = (screenWidth - 20 * 3 - 15 * 2) * 0.25;

    if (freshGoods == null || freshGoods!.isEmpty) {
      return LoadingImgWidget();
    } else {
      return Container(
        color: Colors.white,
        padding: EdgeInsets.all(10),
        child: Column(
          children: [
            // 标题
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  '新鲜好物',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w400,
                    color: Color(0xFF262626),
                  ),
                ),
                Container(width: 10),
                Text(
                  '更多>>',
                  style: TextStyle(fontSize: 12, color: Color(0xFF7f7f7f)),
                ),
              ],
            ),
            // 商品列表
            Padding(
              padding: EdgeInsets.only(top: 10),
              child: _buildContents(freshGoods!),
            ),
          ],
        ),
      );
    }
  }
}
